package com.d3.pomodoro.component.item

import android.graphics.Rect
import com.baozi.treerecyclerview.base.ViewHolder
import com.baozi.treerecyclerview.factory.ItemHelperFactory
import com.baozi.treerecyclerview.item.TreeItem
import com.baozi.treerecyclerview.item.TreeItemGroup
import com.d3.pomodoro.R
import androidx.recyclerview.widget.RecyclerView
import com.d3.pomodoro.model.ViewModel


class FatherItem : TreeItemGroup<ViewModel.TaskGroupItem>() {

    override fun initChild(data: ViewModel.TaskGroupItem): List<TreeItem<*>>? {
        val items = ItemHelperFactory.createItems(data.potatos, this)
        for (i in items.indices) {
            val treeItem = items[i] as TreeItemGroup<*>
            treeItem.isExpand = false
        }
        return items
    }

    override fun getLayoutId(): Int {
        return R.layout.item_task_header
    }

    override fun isCanExpand(): Boolean {
        return true
    }

    override fun onBindViewHolder(holder: ViewHolder) {
        holder.setText(R.id.textTitle, data.itemName)
    }

    override fun getItemOffsets(
        outRect: Rect,
        layoutParams: RecyclerView.LayoutParams,
        position: Int
    ) {
        super.getItemOffsets(outRect, layoutParams, position)
        outRect.bottom = 1
    }
}

class ChildItemFirst : TreeItemGroup<ViewModel.TaskGroupItem.PotatoItem>() {
    override fun initChild(data: ViewModel.TaskGroupItem.PotatoItem?): MutableList<TreeItem<*>> {
        return ItemHelperFactory.createItems(data?.subPotatos, this)
    }//泛型代表绑定的javabean

    //该级具体展示的Layout
    override fun getLayoutId(): Int {
        return R.layout.item_task
    }

    //view和data绑定
    override fun onBindViewHolder(holder: ViewHolder) {
        holder.setText(R.id.textTitle, data.itemName)
    }
}

class ChildItemSecond :
    TreeItem<ViewModel.TaskGroupItem.PotatoItem.SubPotatoItem>() {//泛型代表绑定的javabean

    override fun getLayoutId(): Int {
        return R.layout.item_sub_task
    }

    //绑定操作,
    override fun onBindViewHolder(holder: ViewHolder) {
        holder.setText(R.id.textTitle, data.itemName)
    }

    //这个Item,在RecyclerView的每行所占比,只有RecyclerView设置了GridLayoutManager才会生效.
    //这里之所以用除法,是为了可以做到,只改变GridLayoutManager的总数,无需改变每个Item,当然也可以直接返回一个int值.
    override fun getSpanSize(maxSpan: Int): Int {
        return maxSpan / 6
    }
}